
#include	"SysConfig.h"
#include	"Define.h"
#include	"Port.h"
#include	"Power.h"
#include	"Display.h"
#include	"Model_Option.h"

#ifdef  ST7565P
#include	"ST7565P.h"

//#define ST7565P_WIDTH			86// + 1		// +1 is dummy that serial problem
//#define ST7565P_COLUMN_ADDRESS	0//46	//0
//#define ST7565P_PAGE8_ADDRESS 	0x80 //or  0x01

unsigned char ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 3];
unsigned short	usST7565P_DataBufCnt;

unsigned char ucDisplayBuf0[ST7565P_WIDTH];
unsigned char ucDisplayBuf1[ST7565P_WIDTH];
unsigned char ucDisplayIconBuf[ST7565P_WIDTH];

unsigned char ucST7565P_Seq;

unsigned char ucST7565P_Seq_Timer5msec;

extern unsigned char const *AGC5030Font_GetData_0x0021_0x007E(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0x3131_0x3163(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xAC00_0xB091(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xB098_0xB2E2(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xB2E4_0xB775(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xB77C_0xB9C1(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xB9C8_0xBC11(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xBC14_0xC0A5(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xC0AC_0xC53D(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xC544_0xC78E(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xC790_0xCC27(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xCC28_0xCE6D(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xCE74_0xD0B9(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xD0C0_0xD305(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xD30C_0xD551(unsigned short ucIndex);
extern unsigned char const *AGC5030Font_GetData_0xD558_0xD79D(unsigned short ucIndex);

extern unsigned short AGC5030_GetFontIndex_0x0021_0x007E(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0x3131_0x3163(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xAC00_0xB091(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xB098_0xB2E2(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xB2E4_0xB775(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xB9C8_0xBC11(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xBC14_0xC0A5(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xC0AC_0xC53D(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xC544_0xC78E(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xC790_0xCC27(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xCC28_0xCE6D(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xCE74_0xD0B9(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xD0C0_0xD305(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xD30C_0xD551(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xD558_0xD79D(unsigned short usIndex);
extern unsigned short AGC5030_GetFontIndex_0xB77C_0xB9C1(unsigned short usIndex);

unsigned char const *ucAGC5030_FontTableAddrs;

/*//////////////////////////////////////////////////////////////////////////
// SPI Send Format => LSB First
		Page-0			 Page-1
	0, 1, 2, 3, 4, 5, 6, 7,  || 0, 1, 2, 3
//////////////////////////////////////////////////////////////////////////*/

unsigned char const ucAGC5030_FontDataDefault[] =
{
	6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageCH1[] =
{
	13, 
	0x00, 0x38, 0x00, 0x44, 0x00, 0x44, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x20, 
	0x00, 0x20,	0x00, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0xFC,
	0x00, 0x00
};

unsigned char const ucAGC5030_ImageCH2[] =
{
	13,
	0x00, 0x38, 0x00, 0x44, 0x00, 0x44, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x20,
	0x00, 0x20, 0x00, 0x1C, 0x00, 0x00, 0x00, 0xBC, 0x00, 0xA4, 0x00, 0xE4,
	0x00, 0x00
};

unsigned char const ucAGC5030_ImageCH3[] =
{
	13, 
	0x00, 0x38, 0x00, 0x44, 0x00, 0x44, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x20, 
	0x00, 0x20, 0x00, 0x1C, 0x00, 0x00, 0x00, 0xA4, 0x00, 0xA4, 0x00, 0xFC,
	0x00, 0x00
};

unsigned char const ucAGC5030_ImageCH4[] =
{
	13, 
	0x00, 0x38, 0x00, 0x44, 0x00, 0x44, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x20, 
	0x00, 0x20, 0x00, 0x1C, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x10, 0x00, 0xFC,
	0x00, 0x00
};

unsigned char const ucAGC5030_ImageCH5[] =
{
	13,
	0x00, 0x38, 0x00, 0x44, 0x00, 0x44, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x20, 
	0x00, 0x20, 0x00, 0x1C, 0x00, 0x00, 0x00, 0xE4, 0x00, 0xA4, 0x00, 0xBC, 
	0x00, 0x00
};

unsigned char const ucAGC5030_ImageCH6[] =
{
	13, 
	0x00, 0x38, 0x00, 0x44, 0x00, 0x44, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x20, 
	0x00, 0x20, 0x00, 0x1C, 0x00, 0x00, 0x00, 0xFC, 0x00, 0xA4, 0x00, 0xBC,
	0x00, 0x00
};

unsigned char const ucAGC5030_ImageK[] = 
{
	5,
	0x00, 0xFC, 0x00, 0x30, 0x00, 0x48, 0x00, 0x84, 0x00, 0x00, 0x00, 0x20, 
	0x00, 0x20, 0x00, 0x1C, 0x00, 0x00, 0x00, 0xFC, 0x00, 0xA4, 0x00, 0xBC
};

unsigned char const ucAGC5030_ImageM[] = 
{
	6, 
	0x00, 0xFC, 0x00, 0x40, 0x00, 0x20, 0x00, 0x40, 0x00, 0xFC, 0x00, 0x00, 
	0x00, 0x20, 0x00, 0x1C, 0x00, 0x00, 0x00, 0xFC, 0x00, 0xA4, 0x00, 0xBC
};

unsigned char const ucAGC5030_ImageHz[] = 
{
	11, 
	0x00, 0xFC, 0x00, 0x20, 0x00, 0x20, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x4C, 
	0x00, 0x54, 0x00, 0x64, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBC
};

unsigned char const ucAGC5030_ImageKHz[] =
{
	15, 
	0x00, 0xFC, 0x00, 0x30, 0x00, 0x48, 0x00, 0x84, 0x00, 0x00, 0x00, 0xFC, 
	0x00, 0x20, 0x00, 0x20, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x54,
	0x00, 0x64, 0x00, 0x44, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageMHz[] = 
{
	16, 
	0x00, 0xFC, 0x00, 0x40, 0x00, 0x20, 0x00, 0x40, 0x00, 0xFC, 0x00, 0x00,
	0x00, 0xFC, 0x00, 0x20, 0x00, 0x20, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x4C, 
	0x00, 0x54, 0x00, 0x64, 0x00, 0x44, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageBTIcon[] = 
{
	9, //change size
	0x00, 0x00, 0x01, 0x04, 0x00, 0x88, 0x00, 0x50, 0x07, 0xFF, 0x02, 0x22, 
	0x01, 0x54, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageFolder[] =
{
	12,																		
	0x03, 0xFC, 0x05, 0x04, 0x05, 0x04, 0x05, 0x04, 0x05, 0x04, 0x05, 0x04, 
	0x05, 0x04, 0x05, 0x04, 0x03, 0x04, 0x01, 0x04, 0x01, 0xFC, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageFM[] =
{
	12,																		
	0x00, 0x7E, 0x00, 0x48, 0x00, 0x48, 0x00, 0x48, 0x00, 0x30, 0x00, 0x00, 
	0x00, 0x7E, 0x00, 0x20, 0x00, 0x10, 0x00, 0x20, 0x00, 0x7E, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageAM[] =
{
	12,																		
	0x04, 0x06, 0x04, 0x1A, 0x04, 0x64, 0x00, 0x1C, 0x00, 0x06, 0x00, 0x00, 
	0x00, 0x7E, 0x00, 0x20, 0x00, 0x10, 0x00, 0x20, 0x00, 0x7E, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageZ[] =
{
	5,																		
	0x04, 0x06, 0x04, 0x1A, 0x04, 0x62, 0x05, 0x82, 0x06, 0x02, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageY[] =
{
	5,																		
	0x07, 0x00, 0x00, 0xE0, 0x00, 0x1E, 0x00, 0xE0, 0x07, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageL[] =
{
	5,																		
	0x07, 0xFE, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageE[] =
{
	5,																
	0x07, 0xFE, 0x04, 0x42, 0x04, 0x42, 0x04, 0x42, 0x04, 0x02, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageD[] =
{
	6,																
	0x07, 0xFE, 0x04, 0x02, 0x04, 0x02, 0x04, 0x02, 0x02, 0x04, 0x01, 0xF8, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageA[] =
{
	7,																
	0x00, 0x0E, 0x00, 0x70, 0x01, 0x90, 0x06, 0x10, 0x01, 0x90, 0x00, 0x70, 
	0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
unsigned char const ucAGC5030_ImageW[] =
{
	7,																
	0x07, 0xF8, 0x00, 0x06, 0x00, 0x38, 0x00, 0xC0, 0x00, 0x38, 0x00, 0x06, 
	0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

unsigned char const ucAGC5030_ImageO[] =
{
	6,																
	0x01, 0xF8, 0x02, 0x04, 0x04, 0x02, 0x04, 0x02, 0x02, 0x04, 0x01, 0xF8, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};


unsigned char ST7565P_GetFont(unsigned short usIndex)
{
	unsigned char wth = 0;
	unsigned char i;
	unsigned short usTableIndex;

	ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
			
	if  ((usIndex >= 0x0021) && (usIndex <= 0x007E))
	{
		usTableIndex = AGC5030_GetFontIndex_0x0021_0x007E(usIndex);
		if  (usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0x0021_0x007E(usTableIndex);
		}
	}

	if	((usIndex >= 0x3131) && (usIndex <= 0x3163))
	{
		usTableIndex = AGC5030_GetFontIndex_0x3131_0x3163(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0x3131_0x3163(usTableIndex);
		}
	}
	
	if  ((usIndex >= 0xAC00) && (usIndex <= 0xB091))
	{
		usTableIndex = AGC5030_GetFontIndex_0xAC00_0xB091(usIndex);
		if  (usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xAC00_0xB091(usTableIndex);
		}
	}

	
	if	((usIndex >= 0xB098) && (usIndex <= 0xB2E2))
	{
		usTableIndex = AGC5030_GetFontIndex_0xB098_0xB2E2(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xB098_0xB2E2(usTableIndex);
		}
	}

	if	((usIndex >= 0xB2E4) && (usIndex <= 0xB775))
	{
		usTableIndex = AGC5030_GetFontIndex_0xB2E4_0xB775(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xB2E4_0xB775(usTableIndex);
		}
	}
	
	if	((usIndex >= 0xB77C) && (usIndex <= 0xB9C1))
	{
		usTableIndex = AGC5030_GetFontIndex_0xB77C_0xB9C1(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xB77C_0xB9C1(usTableIndex);
		}
	}

	if	((usIndex >= 0xB9C8) && (usIndex <= 0xBC11))
	{
		usTableIndex = AGC5030_GetFontIndex_0xB9C8_0xBC11(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xB9C8_0xBC11(usTableIndex);
		}
	}

	if	((usIndex >= 0xBC14) && (usIndex <= 0xC0A5))
	{
		usTableIndex = AGC5030_GetFontIndex_0xBC14_0xC0A5(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xBC14_0xC0A5(usTableIndex);
		}
	}
	
	if	((usIndex >= 0xC0AC) && (usIndex <= 0xC53D))
	{
		usTableIndex = AGC5030_GetFontIndex_0xC0AC_0xC53D(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xC0AC_0xC53D(usTableIndex);
		}
	}
	
	if	((usIndex >= 0xC544) && (usIndex <= 0xC78E))
	{
		usTableIndex = AGC5030_GetFontIndex_0xC544_0xC78E(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xC544_0xC78E(usTableIndex);
		}
	}

	if	((usIndex >= 0xC790) && (usIndex <= 0xCC27))
	{
		usTableIndex = AGC5030_GetFontIndex_0xC790_0xCC27(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xC790_0xCC27(usTableIndex);
		}
	}

	if	((usIndex >= 0xCC28) && (usIndex <= 0xCE6D))
	{
		usTableIndex = AGC5030_GetFontIndex_0xCC28_0xCE6D(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xCC28_0xCE6D(usTableIndex);
		}
	}

	if	((usIndex >= 0xCE74) && (usIndex <= 0xD0B9))
	{
		usTableIndex = AGC5030_GetFontIndex_0xCE74_0xD0B9(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xCE74_0xD0B9(usTableIndex);
		}
	}

	if	((usIndex >= 0xD0C0) && (usIndex <= 0xD305))
	{
		usTableIndex = AGC5030_GetFontIndex_0xD0C0_0xD305(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xD0C0_0xD305(usTableIndex);
		}
	}

	if	((usIndex >= 0xD30C) && (usIndex <= 0xD551))
	{
		usTableIndex = AGC5030_GetFontIndex_0xD30C_0xD551(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xD30C_0xD551(usTableIndex);
		}
	}

	if	((usIndex >= 0xD558) && (usIndex <= 0xD79D))
	{
		usTableIndex = AGC5030_GetFontIndex_0xD558_0xD79D(usIndex);
		if	(usTableIndex == 0xFFFF)
		{
			ucAGC5030_FontTableAddrs = &ucAGC5030_FontDataDefault[0];
		}
		else
		{
			ucAGC5030_FontTableAddrs = AGC5030Font_GetData_0xD558_0xD79D(usTableIndex);
		}
	}
	
	wth = *ucAGC5030_FontTableAddrs;
	
	return(wth);
}

void ST7565_DisplayClear(void)
{
	memset(&ucDisplayBuf0[0],0x00,ST7565P_WIDTH);
	memset(&ucDisplayBuf1[0],0x00,ST7565P_WIDTH);
	memset(&ucDisplayIconBuf[0],0x00,ST7565P_WIDTH);
}

unsigned char ST7565_UpdateData(unsigned short usX)
{
	unsigned char wth;
	unsigned char i;

	wth = ucAGC5030_FontTableAddrs[0];
	for(i = 0; i < wth; ++i)
	{
		ucDisplayBuf0[usX + i] = ucAGC5030_FontTableAddrs[i * 2 + 1];
		ucDisplayBuf1[usX + i] = ucAGC5030_FontTableAddrs[i * 2 + 2];
		if  ((usX + i) >= (ST7565P_WIDTH - 1))
		{
			break;
		}
	}

	return(wth);
}

unsigned char ST7565_UpdateDataScroll(unsigned short usX, unsigned short usOffset)
{
	unsigned char wth;
	unsigned char i;

	wth = ucAGC5030_FontTableAddrs[0];
	for(i = 0; i < (wth - usOffset); ++i)
	{
		ucDisplayBuf0[usX + i] = ucAGC5030_FontTableAddrs[(i + usOffset) * 2 + 1];
		ucDisplayBuf1[usX + i] = ucAGC5030_FontTableAddrs[(i + usOffset) * 2 + 2];
		if  ((usX + i) >= (ST7565P_WIDTH - 1))
		{
			break;
		}
	}

	return(wth - usOffset);
}


unsigned char ST7565_UpdateDisplayData(unsigned short usX, unsigned short usBuf)
{
	unsigned char wth;
	unsigned char i;
	
	wth = ST7565P_GetFont(usBuf);
	ST7565_UpdateData(usX);
	return(wth);
}

unsigned char ST7565_UpdateDisplayString(unsigned short usX, unsigned char *usBuf, unsigned char ucAlign)
{
	unsigned short usOffset;
	unsigned char wth;
	unsigned char ucCnt;
	unsigned char ucStrWth;
	unsigned char i;
	
	usOffset = usX;

	if  (ucAlign != 0)
	{
		ucStrWth = 0;
		i = 0;
		while(1)
		{
			if  (*(usBuf + i) == 0x00)
			{
				break;
			}
			ST7565P_GetFont(*usBuf);
			wth = ucAGC5030_FontTableAddrs[0];
			ucStrWth = ucStrWth + wth;
			if  (ucStrWth > (ST7565P_WIDTH + 2))
			{
				break;
			}
			if  (*(usBuf + i) == '1')
			{
				ucStrWth = ucStrWth + 2;
			}
			
			++i;
		}

		if  (ucAlign == 1)
		{
			if  (ucStrWth > ST7565P_WIDTH)
			{
				usOffset = 0;
			}
			else
			{
				ucStrWth = ST7565P_WIDTH - ucStrWth;
				usOffset = ucStrWth / 2;
			}
		}
	}

	ucCnt = 0;
	
	while(1)
	{
		if  (*usBuf == 0x00)
		{
			break;
		}
		ST7565P_GetFont(*usBuf);
		wth = ucAGC5030_FontTableAddrs[0];
		if  ((usOffset + wth) > (ST7565P_WIDTH + 2))
		{
			break;
		}
		usOffset = usOffset + ST7565_UpdateData(usOffset);
		if  (*usBuf == '1')
		{
			usOffset = usOffset + 2;
		}
		
		++usBuf;
		++ucCnt;
	}
	return(ucCnt);
}

unsigned char ST7565_UpdateDisplayStringUnicode(unsigned short usX, unsigned char *usBuf, unsigned char ucAlign, unsigned char ucUni)
{
	unsigned short usOffset;
	unsigned short usTemp;
	unsigned char ucCnt;
	unsigned char wth;

	usOffset = usX;
	ucCnt = 0;

	if  ((ucUni == 0xF0) || (ucUni == 0xF1) || (ucUni == 0xF2) || (ucUni == 0x00))
	{
		ucCnt = ST7565_UpdateDisplayString(usX, usBuf, ucAlign);
		return(ucCnt);	
	}
	while(1)
	{
		if  ((ucUni == 0xF4) || (ucUni == 0xF7) || (ucUni == 0xF9))
		{
			usTemp = *usBuf;
			usTemp = (usTemp << 8) & 0xFF00 | *(usBuf + 1);
		}
		else
		{
			usTemp = *(usBuf + 1);
			usTemp = (usTemp << 8) & 0xFF00 | *usBuf;
		}
		if  (usTemp == 0x00)
		{
			break;
		}

		if  ((ucCnt == 0) && (usTemp == ' '))
		{			
		}
		else
		{
			ST7565P_GetFont(usTemp);
			wth = ucAGC5030_FontTableAddrs[0];
			if  ((usOffset + wth) > (ST7565P_WIDTH + 2))
			{
				break;
			}
			usOffset = usOffset + ST7565_UpdateData(usOffset);
		}
		++ucCnt;
		++ucCnt;
		++usBuf;
		++usBuf;
	}
	
	return(ucCnt);
}

unsigned short ST7565_UpdateDisplayStringUnicodeScroll(unsigned short usX, unsigned short usOffset, unsigned char *ucBuf, unsigned char ucSize, unsigned char ucUni)
{
	unsigned short usTemp = 0;
	unsigned short usDisplayedDotCnt = 0;
	unsigned char wth;
	unsigned char i;
	unsigned short usRemainOffset;
	unsigned char ucStringEnd = 0;
	unsigned char ucCnt = 0;

	usRemainOffset = usOffset;
	
	while(1)
	{
		if  (ucCnt == ucSize)
		{
			ucStringEnd = 1;
			break;
		}
		if  ((ucUni == 0xF0) || (ucUni == 0xF1) || (ucUni == 0xF2) || (ucUni == 0x00))
		{
			usTemp = *ucBuf;
			++ucBuf;
			++ucCnt;
		}
		else
		{
			if  ((ucUni == 0xF4) || (ucUni == 0xF7) || (ucUni == 0xF9))
			{
				usTemp = *ucBuf;
				usTemp = (usTemp << 8) & 0xFF00 | *(ucBuf + 1);
				++ucBuf;
				++ucBuf;
			}
			else
			{
				usTemp = *(ucBuf + 1);
				usTemp = (usTemp << 8) & 0xFF00 | *ucBuf;
				++ucBuf;
				++ucBuf;
			}
			++ucCnt;
			++ucCnt;
		}
		if  (usTemp == 0x00)
		{
			ucStringEnd = 1;
			break;
		}

		ST7565P_GetFont(usTemp);
		wth = ucAGC5030_FontTableAddrs[0];

		if  (usRemainOffset > wth)
		{
			usRemainOffset = usRemainOffset - wth;
		}
		else
		{		
			for(i = 0; i < (wth - usRemainOffset); ++i)
			{
				ucDisplayBuf0[usX + usDisplayedDotCnt] = ucAGC5030_FontTableAddrs[(i + usRemainOffset) * 2 + 1];
				ucDisplayBuf1[usX + usDisplayedDotCnt] = ucAGC5030_FontTableAddrs[(i + usRemainOffset) * 2 + 2];
				++usDisplayedDotCnt;
				if  ((usDisplayedDotCnt + usX) == ST7565P_WIDTH)
				{
					break;
				}
			}
			usRemainOffset = 0;
		}
		
		if	((usDisplayedDotCnt + usX) == ST7565P_WIDTH)
		{
			break;
		}

	}

	if  ((usOffset == 0) && (ucStringEnd == 1))
	{
		return(0xFFFF);
	}
	return(usDisplayedDotCnt);
}


void ST7565_UpdateDisplayImage(unsigned short usX, unsigned char ucIndex)
{
	unsigned short wth = 0;
	unsigned char i;
	unsigned short usOffset;
	unsigned const char *ucTable;
	unsigned short usBuf;

	switch  (ucIndex)
	{
		case IMAGE_CH1:
			wth = ucAGC5030_ImageCH1[0];
			ucTable = &ucAGC5030_ImageCH1[1];
			break;
		case IMAGE_CH2:
			wth = ucAGC5030_ImageCH2[0];
			ucTable = &ucAGC5030_ImageCH2[1];			
			break;
		case IMAGE_CH3:
			wth = ucAGC5030_ImageCH3[0];
			ucTable = &ucAGC5030_ImageCH3[1];
			break;
		case IMAGE_CH4:
			wth = ucAGC5030_ImageCH4[0];
			ucTable = &ucAGC5030_ImageCH4[1];
			break;
		case IMAGE_CH5:
			wth = ucAGC5030_ImageCH5[0];
			ucTable = &ucAGC5030_ImageCH5[1];
			break;
		case IMAGE_CH6:
			wth = ucAGC5030_ImageCH6[0];
			ucTable = &ucAGC5030_ImageCH6[1];
			break;	
		case IMAGE_K:
			wth = ucAGC5030_ImageK[0];
			ucTable = &ucAGC5030_ImageK[1];
			break;
		case IMAGE_M:
			wth = ucAGC5030_ImageM[0];
			ucTable = &ucAGC5030_ImageM[1];
			break;
		case IMAGE_HZ:
			wth = ucAGC5030_ImageHz[0];
			ucTable = &ucAGC5030_ImageHz[1];
			break;		
		case IMAGE_KHZ:
			wth = ucAGC5030_ImageKHz[0];
			ucTable = &ucAGC5030_ImageKHz[1];
			break;
		case IMAGE_MHZ:
			wth = ucAGC5030_ImageMHz[0];
			ucTable = &ucAGC5030_ImageMHz[1];
			break;
		case IMAGE_BT:
			wth = ucAGC5030_ImageBTIcon[0];
			ucTable = &ucAGC5030_ImageBTIcon[1];
			break;
		case IMAGE_FOLDER:
			wth = ucAGC5030_ImageFolder[0];
			ucTable = &ucAGC5030_ImageFolder[1];
			break;
		case IMAGE_AM:
			wth = ucAGC5030_ImageAM[0];
			ucTable = &ucAGC5030_ImageAM[1];
			break;
		case IMAGE_PM:
			wth = ucAGC5030_ImageFM[0];
			ucTable = &ucAGC5030_ImageFM[1];
			break;
		case IMAGE_Z:
			wth = ucAGC5030_ImageZ[0];
			ucTable = &ucAGC5030_ImageZ[1];
			break;
		case IMAGE_Y:
			wth = ucAGC5030_ImageY[0];
			ucTable = &ucAGC5030_ImageY[1];
			break;
		case IMAGE_L:
			wth = ucAGC5030_ImageL[0];
			ucTable = &ucAGC5030_ImageL[1];
			break;
		case IMAGE_E:
			wth = ucAGC5030_ImageE[0];
			ucTable = &ucAGC5030_ImageE[1];
			break;
		case IMAGE_D:
			wth = ucAGC5030_ImageD[0];
			ucTable = &ucAGC5030_ImageD[1];
			break;
		case IMAGE_A:
			wth = ucAGC5030_ImageA[0];
			ucTable = &ucAGC5030_ImageA[1];
			break;
		case IMAGE_W:
			wth = ucAGC5030_ImageW[0];
			ucTable = &ucAGC5030_ImageW[1];
			break;
		case IMAGE_O:
			wth = ucAGC5030_ImageO[0];
			ucTable = &ucAGC5030_ImageO[1];
			break;
	}
	
	if	((usX + wth) > (ST7565P_WIDTH + 1))
	{
		return;
	}

	for(i = 0; i < wth; ++i)
	{		
		ucDisplayBuf0[usX + i] = *(ucTable + i * 2);
		ucDisplayBuf1[usX + i] = *(ucTable + i * 2 + 1);
	}
}

unsigned short const usST7565_SubFontData[] =
{
	SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_J,			// A 0x41
	SEG_A | SEG_B | SEG_C | SEG_D | SEG_H | SEG_N | SEG_J,	// B  
	SEG_A | SEG_F | SEG_E | SEG_D,							// C
	SEG_A | SEG_B | SEG_C | SEG_D | SEG_H | SEG_N,			// D
	SEG_A | SEG_D | SEG_E | SEG_F | SEG_J,					// E
	SEG_A | SEG_F | SEG_E | SEG_J,							// F
	SEG_A | SEG_F | SEG_E | SEG_D | SEG_C | SEG_J,			// G
	SEG_B | SEG_C | SEG_F | SEG_E | SEG_J,					// H
	SEG_A | SEG_H | SEG_N | SEG_D,							// I
	SEG_E | SEG_D | SEG_C | SEG_B,							// J
	SEG_F | SEG_E | SEG_I | SEG_L,							// K
	SEG_F | SEG_E | SEG_D,									// L
	SEG_F | SEG_E | SEG_B | SEG_C | SEG_G | SEG_I,			// M
	SEG_F | SEG_E | SEG_G | SEG_L | SEG_B | SEG_C,			// N
	SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,			// O
	SEG_A | SEG_B | SEG_J | SEG_F | SEG_E,					// P
	SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_L,	// Q
	SEG_A | SEG_B | SEG_J | SEG_F | SEG_E | SEG_L,			// R
	SEG_A | SEG_F | SEG_J | SEG_C | SEG_D,					// S
	SEG_A | SEG_H | SEG_N,									// T
	SEG_F | SEG_B | SEG_E | SEG_C | SEG_D,					// U
	SEG_F | SEG_E | SEG_K | SEG_I,							// V
	SEG_F | SEG_E | SEG_K | SEG_L | SEG_C | SEG_B,			// W
	SEG_G | SEG_I | SEG_K | SEG_L,							// X
	SEG_F | SEG_J | SEG_B | SEG_C | SEG_D,					// Y
	SEG_A | SEG_I | SEG_K | SEG_D,							// Z
};
unsigned short const usST7565_SubNumFontData[] =
{
	SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G | SEG_L,		// 0
	SEG_B | SEG_C, 														// 1
	SEG_A | SEG_B | SEG_J | SEG_E | SEG_D,	 							// 2
	SEG_A | SEG_B | SEG_J | SEG_C | SEG_D,	 							// 3
	SEG_B | SEG_C | SEG_F | SEG_J,	 									// 4
	SEG_A | SEG_F | SEG_J | SEG_C | SEG_D,	 							// 5
	SEG_A | SEG_F | SEG_E | SEG_D | SEG_C | SEG_J,	 					// 6
	SEG_A | SEG_B | SEG_C,	 											// 7
	SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_J,	 			// 8
	SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_J,	 					// 9
};

unsigned short ST7565_GetSubFont(unsigned char ucData)
{
	if  ((ucData >= 'A') && (ucData <= 'Z'))
	{
		return(usST7565_SubFontData[ucData - 'A']);
	}
	if  ((ucData >= 'a') && (ucData <= 'z'))
	{
		return(usST7565_SubFontData[ucData - 'a']);
	}
	if  ((ucData >= '0') && (ucData <= '9'))
	{
		return(usST7565_SubNumFontData[ucData - '0']);
	}
	return(0);
}
void ST7565_MakeSmallFont(unsigned char ucLoc, unsigned short usFont)
{
	if  (ucLoc == 0)	//2k ~ 2d
	{		
		if  (usFont & SEG_J)
		{
			ucDisplayIconBuf[51] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_B)
		{
			ucDisplayIconBuf[52] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_I)
		{
			ucDisplayIconBuf[54] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_A)
		{
			ucDisplayIconBuf[55] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_H)
		{
			ucDisplayIconBuf[56] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_G)
		{
			ucDisplayIconBuf[57] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_F)
		{
			ucDisplayIconBuf[59] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_E)
		{
			ucDisplayIconBuf[60] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_K)
		{
			ucDisplayIconBuf[61] = ST7565P_PAGE8_ADDRESS;
		}
		if	(usFont & SEG_D)
		{
			ucDisplayIconBuf[62] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_N)
		{
			ucDisplayIconBuf[63] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_L)
		{
			ucDisplayIconBuf[64] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_C)
		{
			ucDisplayIconBuf[66] = ST7565P_PAGE8_ADDRESS;
		}
		
	}

	if  (ucLoc == 1) //3k ~3d
	{		
		if  (usFont & SEG_F)
		{
			ucDisplayIconBuf[1] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_E)
		{
			ucDisplayIconBuf[2] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_K)
		{
			ucDisplayIconBuf[3] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_D)
		{
			ucDisplayIconBuf[4] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_N)
		{
			ucDisplayIconBuf[5] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_L)
		{
			ucDisplayIconBuf[6] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_C)
		{
			ucDisplayIconBuf[7] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_J)
		{
			ucDisplayIconBuf[8] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_B)
		{
			ucDisplayIconBuf[9] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_I)
		{
			ucDisplayIconBuf[13] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_H)
		{
			ucDisplayIconBuf[15] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_A)
		{
			ucDisplayIconBuf[49] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_G)
		{
			ucDisplayIconBuf[50] = ST7565P_PAGE8_ADDRESS;
		}		
	}
	
	if  (ucLoc == 2) //4k ~4d
	{
		if  (usFont & SEG_F)
		{
			ucDisplayIconBuf[16] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_E)
		{
			ucDisplayIconBuf[17] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_K)
		{
			ucDisplayIconBuf[18] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_D)
		{
			ucDisplayIconBuf[20] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_N)
		{
			ucDisplayIconBuf[23] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_L)
		{
			ucDisplayIconBuf[26] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_C)
		{
			ucDisplayIconBuf[27] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_J)
		{
			ucDisplayIconBuf[28] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_B)
		{
			ucDisplayIconBuf[29] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_I)
		{
			ucDisplayIconBuf[31] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_H)
		{
			ucDisplayIconBuf[35] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_A)
		{
			ucDisplayIconBuf[34] = ST7565P_PAGE8_ADDRESS;
		}
		if  (usFont & SEG_G)
		{
			ucDisplayIconBuf[36] = ST7565P_PAGE8_ADDRESS;
		}
	}
}

void ST7565_UpdateSubDisplayData(unsigned char *ucBuf)
{
	ST7565_MakeSmallFont(0, ST7565_GetSubFont(*(ucBuf + 0)));
	ST7565_MakeSmallFont(1, ST7565_GetSubFont(*(ucBuf + 1)));
	ST7565_MakeSmallFont(2, ST7565_GetSubFont(*(ucBuf + 2)));
}


void ST7565_UpdateIconData(unsigned long ulData)
{
	if  (ulData & ICON_MASK_WMA)
	{
		ucDisplayIconBuf[10] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_ST)
	{
		ucDisplayIconBuf[11] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_MP3)
	{
		ucDisplayIconBuf[12] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_RDS)
	{
		ucDisplayIconBuf[14] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_REG)
	{
		ucDisplayIconBuf[19] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_AUX)
	{
		ucDisplayIconBuf[21] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_USB)
	{
		ucDisplayIconBuf[22] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_BT)
	{
		ucDisplayIconBuf[30] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_PTY)
	{
		ucDisplayIconBuf[31] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_AF)
	{
		ucDisplayIconBuf[33] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_SCH)
	{
		ucDisplayIconBuf[40] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_DIR)
	{
		ucDisplayIconBuf[45] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_INT)
	{
		ucDisplayIconBuf[46] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_RPT)
	{
		ucDisplayIconBuf[53] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_RDM)
	{
		ucDisplayIconBuf[58] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_ALL)
	{
		ucDisplayIconBuf[65] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_DRV)
	{
		ucDisplayIconBuf[69] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_PSG)
	{
		ucDisplayIconBuf[74] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_LOUD)
	{
		ucDisplayIconBuf[80] = ST7565P_PAGE8_ADDRESS;
	}
	if  (ulData & ICON_MASK_EQ)
	{
		ucDisplayIconBuf[85] = ST7565P_PAGE8_ADDRESS;
	}
		
///////////////////////////////////////////////////////////
	if  (ulData & ICON_MASK_CDDET_X)
	{
		ucDisplayIconBuf[42] = ST7565P_PAGE8_ADDRESS;
	}	

	if	(ulData & ICON_MASK_CDDET_X1)
	{
		ucDisplayIconBuf[39] = ST7565P_PAGE8_ADDRESS;
	}
	
	if	(ulData & ICON_MASK_CDDET_X2)
	{
		ucDisplayIconBuf[38] = ST7565P_PAGE8_ADDRESS;
	}
	
	if	(ulData & ICON_MASK_CDDET_X3)
	{
		ucDisplayIconBuf[37] = ST7565P_PAGE8_ADDRESS;
	}
	
	if	(ulData & ICON_MASK_CDDET_X4)
	{
		ucDisplayIconBuf[41] = ST7565P_PAGE8_ADDRESS;
	}
	
	if	(ulData & ICON_MASK_CDDET_X5)
	{
		ucDisplayIconBuf[48] = ST7565P_PAGE8_ADDRESS;
	}
	
	if	(ulData & ICON_MASK_CDDET_X6)
	{
		ucDisplayIconBuf[47] = ST7565P_PAGE8_ADDRESS;
	}
	
	if	(ulData & ICON_MASK_CDDET_X7)
	{
		ucDisplayIconBuf[44] = ST7565P_PAGE8_ADDRESS;
	}
	
	if	(ulData & ICON_MASK_CDDET_X8)
	{
		ucDisplayIconBuf[43] = ST7565P_PAGE8_ADDRESS;
	}


}

void ST7565_SerialTX_Interrupt(void)
{
	++usST7565P_DataBufCnt;
	if  ((usST7565P_DataBufCnt % (ST7565P_WIDTH + 3)) < 3)
	{
		uform_u2c0 = 1;
		PO_ST7565_A0 = 0;	// cmd
	}
	else
	{
		PO_ST7565_A0 = 1;	// Data
		uform_u2c0 = 0;
	}

	if  (usST7565P_DataBufCnt > ((ST7565P_WIDTH + 3) * 3 - 1))
	{
		te_u2c1 = 0;	/* U0C1 register re-setup	*/
							/* <TE>: transmit enable	*/
		s2tic = 0;		// Interrupt Disable : Level 6
		PO_ST7565_CS = 1;
		if  (ucST7565P_Seq == ST7565P_SEQ_SEND_WAIT)
		{
			ucST7565P_Seq = ST7565P_SEQ_SEND_RESTART;
		}
		else  if  (ucST7565P_Seq  == ST7565P_SEQ_LCD_CLEAR_WAIT)
		{
			ucST7565P_Seq  = ST7565P_SEQ_DISPLAY_ON;
		}
	}
	else
	{
		u2tb = ucST7565P_DataBuf[usST7565P_DataBufCnt];
	}
}

void ST7565_DMA0_Interrupt(void)
{
	dm0con = dm0con & 0xF7;		/* <DMAE>: DMA disable	*/
	if  (ST7565P_SEQ_SEND_WAIT == ucST7565P_Seq)
	{
		ucST7565P_Seq = ST7565P_SEQ_SEND_RESTART;
	}
}

void ST7565_Delay(unsigned int ucCnt)
{
	unsigned char i;

	for(i = 0; i < ucCnt; ++i)
	{
		M_NOP;
	}
}

void ST7565P_DMAC0_SetSourcePointerAddress(unsigned char ucPage)
{
	if  (ucPage == 0xFF)
	{
		sar0_addr.byte.low = (char)(&ucST7565P_DataBuf[1]);
		sar0_addr.byte.mid = (char)((unsigned long)(&ucST7565P_DataBuf[1]) >> 8);
		sar0_addr.byte.high = (char)((unsigned long)(&ucST7565P_DataBuf[1]) >> 16);
		tcr0 = ((ST7565P_WIDTH + 3) * 3 - 1) - 1;
		return;
	}

	if  (ucPage == 2)
	{
		sar0_addr.byte.low = 0x00;
		sar0_addr.byte.mid = 0x00;
		sar0_addr.byte.high = 0x00;
	}
}

void ST7565_SendCommand(unsigned char ucData)	// A0 Low
{
	unsigned int i;
	
	uform_u2c0 = 1;
	PO_ST7565_CS = 0;
	PO_ST7565_A0 = 0;

	M_NOP;
	M_NOP;
	
	u2tb = ucData;
	te_u2c1 = 1;

	for(i = 0; i < 5000; ++i)
	{
		if  (ti_u2c1 == 1)
		{
			break;
		}
	}
	for(i = 0; i < 5000; ++i)
	{
		if  (txept_u2c0 == 1)
		{
			break;
		}
	}
	
	M_NOP;
	M_NOP;
	
	PO_ST7565_CS = 1;
	te_u2c1 = 0;
}

void ST7565_SendData(unsigned char ucData)
{
	unsigned char i;

	uform_u2c0 = 0;
	PO_ST7565_A0 = 1;
	PO_ST7565_CS = 0;

	M_NOP;
	M_NOP;
	
	dm0con |= 0x08;	/* Set DM0CON register	*/
					/* <DMAE>: DMA enable	*/
	te_u2c1 = 1;	/* U0C1 register re-setup	*/
					/* <TE>: transmit enable	*/
	s2tic = 6;		// Interrupt Disable : Level 6
	u2tb = ucData;
}

void ST7565_SendData_NoneInt(unsigned char ucData)	// A0 Low
{
	unsigned int i;
	PO_ST7565_CS = 0;
	PO_ST7565_A0 = 1;
	
	ST7565_Delay(2);
	
	u2tb = ucData;
	te_u2c1 = 1;

	for(i = 0; i < 5000; ++i)
	{
		if  (ti_u2c1 == 1)
		{
			break;
		}
	}
	for(i = 0; i < 5000; ++i)
	{
		if  (txept_u2c0 == 1)
		{
			break;
		}
	}
	
	ST7565_Delay(100);
	
	PO_ST7565_CS = 1;
	te_u2c1 = 0;
}

#define	ST7565P_BIAS	0x01	//0x02	// 0x03
#define	ST7565P_EV		20//24		//27		// 0 ~ 63

#ifdef  ST_EV_ADJ

unsigned char ucST7565_EV_Level;
unsigned char ucST7565_EV_LevelPre;

#endif

void ST7565P_Main(void)
{
	if  (ucST7565P_Seq_Timer5msec != 0x00)
	{
		return;
	}

	switch  (ucST7565P_Seq)
	{
		case ST7565P_SEQ_INIT_START:		// Init
		///////////////////////////// Serial 2 : ST7565  /////////////////////////////////
			u2mr = 0x01;	// Serial I/O mode select bit[0,1,2] : (001) Clock synchronous serial I/O mode
			u2c0 = 0x90;	// UiBRG count source select bit [0,1] : (01) f8SIO is selected
							// Transmit register empty flag[3] : (1) No data present in transmit register(transmission completed)
							// Transfer format select bit[7] : (1) MSB First
			u2brg = 30;
			u2c1 = 0x11;
			u2smr2 = 0x00;
			u2smr3 = 0x00 | 0x02;
			u2smr4 = 0x00;
		//////////////////////////////////////////////////////////////////////////////
			LOG_LCD("ST7565P_Main:%d",ucST7565P_Seq);
			PO_ST7565_CS = 1;
			PO_ST7565_RST = 0;
			ucST7565P_Seq_Timer5msec = 20/5;
			ucST7565P_Seq = ST7565P_SEQ_INIT_START + 1;
			break;
		case ST7565P_SEQ_INIT_START + 1:
			LOG_LCD("ST7565P_Main:%d",ucST7565P_Seq);
			PO_ST7565_RST = 1;
			ucST7565P_Seq_Timer5msec = 50/5;
			ucST7565P_Seq = ST7565P_SEQ_INIT_START + 2;
			break;
		case ST7565P_SEQ_INIT_START + 2:	// Setup data send
			LOG_LCD("ST7565P_Main:%d",ucST7565P_Seq);
			ST7565_SendCommand(0xE2);
			ucST7565P_Seq_Timer5msec = 10/5;
			ucST7565P_Seq = ST7565P_SEQ_INIT_START + 3;
			break;
		case ST7565P_SEQ_INIT_START + 3:	
			LOG_LCD("ST7565P_Main:%d",ucST7565P_Seq);
			ST7565_SendCommand(0xA0 | ST7565P_BIAS);	//  BS=0 ,1/6 BIAS
			ST7565_SendCommand(0xA0);	//  ADC=0 ,SEG0 ->SEG85
			ST7565_SendCommand(0xC8);	//  SHL=1 ,COM63->C52	????????????
			ST7565_SendCommand(0x23);	//

			ST7565_SendCommand(0x81);	//    
			ST7565_SendCommand(0x18);  // vop 7.3V
			ST7565_SendCommand(0xF8);	// Set Booster
			ST7565_SendCommand(0x00);	// Set Booster Data
			ST7565_SendCommand(0x2C);	//
			ucST7565P_Seq_Timer5msec = 10/5;
			ucST7565P_Seq = ST7565P_SEQ_INIT_START + 4;
			break;
		case ST7565P_SEQ_INIT_START + 4:
			LOG_LCD("ST7565P_Main:%d",ucST7565P_Seq);
			ST7565_SendCommand(0x2E);  //  
			ucST7565P_Seq_Timer5msec = 10/5;
			ucST7565P_Seq = ST7565P_SEQ_INIT_START + 5;
			break;
		case ST7565P_SEQ_INIT_START + 5:
			LOG_LCD("ST7565P_Main:%d",ucST7565P_Seq);
			ST7565_SendCommand(0x2F);
			ucST7565P_Seq_Timer5msec = 10/5;
			ucST7565P_Seq = ST7565P_SEQ_INIT_START + 6;
			break;
		case ST7565P_SEQ_INIT_START + 6:
			LOG_LCD("ST7565P_Main:%d",ucST7565P_Seq);
			ST7565_SendCommand(0xA6);//
			ST7565_SendCommand(0x44);//0x40);//	Set Start Line
			
			ST7565_SendCommand(0x81);	// Set Electronic Volume Level
			ST7565_SendCommand(ST7565P_EV);
		#ifdef  ST_EV_ADJ
			ucST7565_EV_Level = ST7565P_EV;
		#endif
			
		//	ucST7565P_Seq = ST7565P_SEQ_LCD_CLEAR;
			ucST7565P_Seq = ST7565P_SEQ_DISPLAY_ON;
			break;
		case ST7565P_SEQ_LCD_CLEAR:
			LOG_LCD("ST7565P_Main:%d",ucST7565P_Seq);
			usST7565P_DataBufCnt = 0;
			ucST7565P_DataBuf[0] = 0xB0 | 0;
			ucST7565P_DataBuf[1] = 0x10 | ((ST7565P_COLUMN_ADDRESS >> 4) & 0x0F);
			ucST7565P_DataBuf[2] = 0x00 | (ST7565P_COLUMN_ADDRESS & 0x0F);
			memset(&ucST7565P_DataBuf[3], 0x00, ST7565P_WIDTH);
			
			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 1] = 0xB0 | 1;
			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 1 + 1] = 0x10 | ((ST7565P_COLUMN_ADDRESS >> 4) & 0x0F);
			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 1 + 2] = 0x00 | (ST7565P_COLUMN_ADDRESS & 0x0F);			
			memset(&ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 1 + 3], 0x00, ST7565P_WIDTH);

			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 2] = 0xB0 | 8;
			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 2 + 1] = 0x10 | ((ST7565P_COLUMN_ADDRESS >> 4) & 0x0F);
			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 2 + 2] = 0x00 | (ST7565P_COLUMN_ADDRESS & 0x0F);			
			memset(&ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 2 + 3],0x00, ST7565P_WIDTH - 1);

			uform_u2c0 = 1;
			PO_ST7565_A0 = 0;
			PO_ST7565_CS = 0;

			M_NOP;
			M_NOP;
			
			te_u2c1 = 1;	/* U0C1 register re-setup	*/
							/* <TE>: transmit enable	*/
			s2tic = 6;		// Interrupt Disable : Level 6
			u2tb = ucST7565P_DataBuf[0];
			ucST7565P_Seq = ST7565P_SEQ_LCD_CLEAR_WAIT;
			break;
		case ST7565P_SEQ_DISPLAY_ON:
			LOG_LCD("ST7565P_Main:%d",ucST7565P_Seq);
			ST7565_SendCommand(0xAF);//	Display On : Normal Display Mode
			ucST7565P_Seq = ST7565P_SEQ_SEND_DATA;
			if  ((OPT_GetOption(OPT_MASK_CLOCK)) || (OPT_GetOption(OPT_MASK_LOGO)))
			{
				PWR_PowerControl(PWR_BACKLIGHT, 1);
			}
			break;
		case ST7565P_SEQ_SEND_DATA:
	//		LOG_LCD("ST7565P_SEQ_PAGE0_SEND_START");

			usST7565P_DataBufCnt = 0;
			ucST7565P_DataBuf[0] = 0xB0 | 0;
			ucST7565P_DataBuf[1] = 0x10 | ((ST7565P_COLUMN_ADDRESS >> 4) & 0x0F);
			ucST7565P_DataBuf[2] = 0x00 | (ST7565P_COLUMN_ADDRESS & 0x0F);
			memcpy(&ucST7565P_DataBuf[3], &ucDisplayBuf0[0], ST7565P_WIDTH);
			
			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 1] = 0xB0 | 1;
			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 1 + 1] = 0x10 | ((ST7565P_COLUMN_ADDRESS >> 4) & 0x0F);
			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 1 + 2] = 0x00 | (ST7565P_COLUMN_ADDRESS & 0x0F);			
			memcpy(&ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 1 + 3], &ucDisplayBuf1[0], ST7565P_WIDTH);

			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 2] = 0xB0 | 8;
			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 2 + 1] = 0x10 | ((ST7565P_COLUMN_ADDRESS >> 4) & 0x0F);
			ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 2 + 2] = 0x00 | (ST7565P_COLUMN_ADDRESS & 0x0F);			
			memcpy(&ucST7565P_DataBuf[(ST7565P_WIDTH + 3) * 2 + 3], &ucDisplayIconBuf[1], ST7565P_WIDTH - 1);

		//	ST7565P_DMAC0_SetSourcePointerAddress(0xFF);
			
			uform_u2c0 = 1;
			PO_ST7565_A0 = 0;
			PO_ST7565_CS = 0;

			M_NOP;
			M_NOP;
			
		//	dm0con |= 0x08;	/* Set DM0CON register	*/
							/* <DMAE>: DMA enable	*/
			te_u2c1 = 1;	/* U0C1 register re-setup	*/
							/* <TE>: transmit enable	*/
			s2tic = 6;		// Interrupt Disable : Level 6
			u2tb = ucST7565P_DataBuf[0];
			ucST7565P_Seq = ST7565P_SEQ_SEND_WAIT;
			break;
		case ST7565P_SEQ_SEND_RESTART:
	//		LOG_LCD("ST7565P_SEQ_SEND_RESTART");
			ucST7565P_Seq = ST7565P_SEQ_SEND_DATA;
		#ifdef  ST_EV_ADJ
			if  (ucST7565_EV_LevelPre != ucST7565_EV_Level)
			{
				ST7565_SendCommand(0x81);	// Set Electronic Volume Level
				ST7565_SendCommand(ucST7565_EV_Level);
				ucST7565_EV_LevelPre = ucST7565_EV_Level;
			}
		#endif
		//	ucST7565P_Seq_Timer10msec = 100/10;
			break;
		case ST7565P_SEQ_IDLE:
			break;
	}
}

void ST7565P_DisplayOff(void)
{
	PO_ST7565_RST = 0;
	ucST7565P_Seq = ST7565P_SEQ_IDLE;
}

void ST7565P_DisplayOn(void)
{
	if  (ucST7565P_Seq == ST7565P_SEQ_IDLE)
	{
		ucST7565P_Seq = ST7565P_SEQ_INIT_START;
	}
}
#endif

